home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / MEMMON_F / MMMETA.C < prev    next >
Text File  |  1990-03-02  |  4KB  |  166 lines

  1. /*
  2.  * mmmeta.c: batch mode metafile(5) translator.
  3.  */
  4.  
  5. #include "memmon.h"
  6.  
  7. hidden novalue mffhex Params((unsigned char *p, int x, int y, int w, int h));
  8. hidden novalue mffcolor Params((int c, char *text));
  9.  
  10. #define CharHeight (textrow / 2)    /* height of a text char */
  11. #define CharBase   (textrow / 5)    /* offset to baseline within text box */
  12. #define CharProp   ((150 * width) / (TextLength * CharHeight))
  13.                     /* proportionality */
  14.  
  15. static char hexo[MapSize][4];        /* color map entries as 3 hex chars */
  16.  
  17. /*
  18.  * devsetup() - set globals to device-dependent values.
  19.  */
  20. novalue devsetup()
  21.    {
  22.    granularity = 4;
  23.    width = 912;
  24.    height = 630;
  25.    textrow = 20;
  26.    textsep = 2;
  27.    memrow = 20;
  28.    batchmode = 1;
  29.    }
  30.  
  31. /*
  32.  * devinit() - initialize output file.
  33.  */
  34. novalue devinit()
  35.    {
  36.    char time_buf[26];
  37.  
  38.    getctime(time_buf);
  39.    printf("%% Icon MemMon snapshots\n");
  40.    if (title)
  41.       printf("%% %s\n", title);
  42.    printf("%% %s", time_buf);
  43.    printf("\n");
  44.    printf("1 metafile\n");
  45.    }
  46.  
  47. /*
  48.  * batbegin() - prepare to write an image.
  49.  */
  50. novalue batbegin()
  51.    {
  52.    int i;
  53.  
  54.    for (i = 0; i < MapSize; i++)  {    /* init hex color map */
  55.       hexo[i][0] = "0123456789ABCDEF"[cmap[i].red>>4];
  56.       hexo[i][1] = "0123456789ABCDEF"[cmap[i].green>>4];
  57.       hexo[i][2] = "0123456789ABCDEF"[cmap[i].blue>>4];
  58.       }
  59.    printf("\n%d %d ", width-1, height-1);
  60.    mffcolor(C_Background, "init\n");    /* start new page/frame/etc. */
  61.    if (textrow > 0)
  62.       printf("%d %d 100 (Helvetica) font\n", CharHeight, CharProp);
  63.    }
  64.  
  65. /*
  66.  * battext(s, row, col, fg, bg) - output text string.
  67.  */
  68. novalue battext(s, row, col, fg, bg)
  69. char *s;
  70. int row, col, fg, bg;
  71.    {
  72.    float charwidth;
  73.    int x, xx, y;
  74.  
  75.    charwidth = (float)width / (float)TextLength;
  76.    x = charwidth * col;
  77.    xx = charwidth * (col + strlen(s) + 1);
  78.    y = height - (row + 1) * textrow + 1;
  79.    mffcolor(bg, "color ");
  80.    printf("%d %d begin %d %d line %d %d line %d %d line fill\n",
  81.       x, y, x, y + textrow - 2, xx - 2, y + textrow - 2, xx - 2, y);
  82.    x += charwidth / 2;
  83.    y += CharBase;
  84.    mffcolor(fg, "color ");
  85.    printf("%d %d ", x, y);
  86.    pstext(s);
  87.    printf(" text\n");
  88.    }
  89.  
  90. /*
  91.  * batmem(mbuf) - output memory image.
  92.  */
  93. novalue batmem(mbuf)
  94. unsigned char mbuf[];
  95.    {
  96.    word n;
  97.    int w, y;
  98.  
  99.    y = memheight;
  100.    n = mempixels;
  101.    while (n > 0 && (y -= memrow) >= 0)  {
  102.       w = (n > width) ? (word)width : n;
  103.          /* the cast above avoids a Lightspeed C 2.0 bug */
  104.       mffhex(mbuf, 0, y, w, memrow-1);
  105.       mbuf += w;
  106.       n -= w;
  107.       }
  108.    }
  109.  
  110. /*
  111.  * batterm() - terminate entire run.  (Nothing to do.)
  112.  */
  113. novalue batterm()
  114.    {
  115.    }
  116.  
  117. /*
  118.  * mffcolor(c, text) - output mff color spec, then the text.
  119.  */
  120. static novalue mffcolor(c, text)
  121. int c;
  122. char *text;
  123.    {
  124.    printf("%3d %3d %3d %s", cmap[c].red, cmap[c].green, cmap[c].blue, text);
  125.    }
  126.  
  127. /*
  128.  * mffhex(buf, x, y, w, h) - output one line of raster data in hex.
  129.  */
  130. static novalue mffhex(buf, x, y, w, h)
  131. unsigned char *buf;
  132. int x, y, w, h;
  133.    {
  134.    register int i;
  135.    register char *o;
  136.    register unsigned char *p, *q;
  137.  
  138.    /*
  139.     *  Check for a line that's all the same color, and output more compactly.
  140.     */
  141.    p = buf;
  142.    i = *p;
  143.    for (q = p + w; q > p && *--q == i; )
  144.       ;
  145.    if (q == p) {
  146.       printf("%d %d %d %d 1 1 -4 raster\n%s0\n", x, y, w, h, hexo[i]);
  147.       return;
  148.       }
  149.  
  150.    /*
  151.     *  Optimization failed. Output line in detail.
  152.     */
  153.    printf("%d %d %d %d %d 1 -4 raster", x, y, w, h, w);
  154.    for (i = 0; i < w; i++)  {
  155.       if ((i & 31) == 0)
  156.          putchar('\n');
  157.       o = hexo[*p++];
  158.       putchar(*o++);    /* red */
  159.       putchar(*o++);    /* green */
  160.       putchar(*o);    /* blue */
  161.       }
  162.    if (w % 2)
  163.       putchar('0');    /* must have an even number of hex digits per raster */
  164.    putchar('\n');
  165.    }
  166.